package cameraController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Camera controller is the class responsible for communication with IP camera using HTTP requests.
 * @author Moamen Mokhtar
 *
 */
public class CameraController {
	URL basicUrl;
	public CameraController(String ip) {
		String url = "http://"+ip+"/cgi-bin/camctrl/camctrl.cgi?";
		try {
			basicUrl= new URL(url);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setPanSpeed(int speed){
		try{
			URL url = new URL(basicUrl.toString()+"speedpan="+speed);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;
		    while ((inputLine = in.readLine()) != null) {
		        System.out.println(inputLine);
		    }
		    in.close();
		    
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
		
	public void zoomOut(){
		try{
			URL url = new URL(basicUrl.toString()+"zoom=wide");
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;
		    while ((inputLine = in.readLine()) != null) {
		        System.out.println(inputLine);
		    }
		    in.close();
		    
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void zoomIn(){
		try{
			URL url = new URL(basicUrl.toString()+"zoom=tele");
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;
		    while ((inputLine = in.readLine()) != null) {
		        System.out.println(inputLine);
		    }
		    in.close();	    
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void moveHome(){
		try {
	        URL url = new URL(basicUrl.toString()+"move=home");
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        String inputLine;
	        while ((inputLine = in.readLine()) != null) {
	            System.out.println(inputLine);
	        }
	        in.close();
	        
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	}
	public void moveUp(){
		try {
	        URL url = new URL(basicUrl.toString()+"move=up");
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        String inputLine;
	
	        while ((inputLine = in.readLine()) != null) {
	            System.out.println(inputLine);
	        }
	        in.close();
	//      Thread.sleep(1000);
	     } catch (IOException e) {
	         e.printStackTrace();
	     }
	//  	   catch(InterruptedException ex){ex.printStackTrace();}
	}
	public void moveDown(){
		try {
	        URL url = new URL(basicUrl.toString()+"move=down");
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        String inputLine;
	        while ((inputLine = in.readLine()) != null) {
	            System.out.println(inputLine);
	        }
	        in.close();
	        
	//      Thread.sleep(1000);
	     } catch (IOException e) {
	         e.printStackTrace();
	     }
	//  	   catch(InterruptedException ex){ex.printStackTrace();}
	}
	public void moveLeft(int speed){
		try {
	        URL url = new URL(basicUrl.toString()+"move=left");
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        String inputLine;
	        while ((inputLine = in.readLine()) != null) {
	            System.out.println(inputLine);
	        }
	        in.close();
	       } catch (Exception e) {
	           e.printStackTrace();
	       }
	        
	}
	public void moveRight(int speed){
		try {
	        URL url = new URL(basicUrl.toString()+"move=right");
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        String inputLine;
	        while ((inputLine = in.readLine()) != null) {
	            System.out.println(inputLine);
	        }
	        in.close();
	        
	     } catch (Exception e) {
	         e.printStackTrace();
	     }
	}
	
}
